<?php

namespace App\Home\Requests;

use App\Admin\Models\SystemModel;
use App\Home\Models\WorshipBibleChapter;
use Illuminate\Support\Arr;

class WorshipBibleChapterRequest extends FormRequest
{
public $validationRules = <<< EOF
{
    "volume_id": "required",
    "chapter_id": "required",
    "verse_id": "required",
    "lection": "max:255",
    "sound_begin": "max:10",
    "sound_end": "max:10"
}
EOF;   

    public function rules()
    {
        $worshipBibleChapter = app(WorshipBibleChapter::class);
        $rules = json_decode($this->validationRules, true);
        $id = (int) optional($this->route('worship-bible-chapters'))->id;
        $keys = $this->keys();
        if ($this->isMethod('get')) {
            $rules = Arr::only($rules, ['input']);
        }
        if ($this->isMethod('post')) {
            if (in_array('input', $keys)) {
                $rules['input'] .= '|unique:'.$worshipBibleChapter->getTable().',input,'.$id;
            }
        }
        if ($this->isMethod('put')) {
            $rules = Arr::only($rules, $keys);
            if (in_array('input', $keys)) {
                $rules['input'] .= '|unique:'.$worshipBibleChapter->getTable().',input,'.$id;
            }
        }
        return $rules;
    }

    public function messages()
    {
        return [];
    }

    public function attributes()
    {
        $systemModel = app(SystemModel::class);
        $worshipBibleChapter = app(WorshipBibleChapter::class);
        return $systemModel->fieldAttributes($worshipBibleChapter->getTable());
    }
}
